package com.consumer.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.consumer.asyncTask.ExecuteTask;
import com.consumer.common.MinioUtil;
import com.consumer.config.URLConfig;
import com.consumer.entity.DevopsBucket;
import com.consumer.entity.DirectoryVo;
import com.consumer.entity.TPlane;
import com.consumer.entity.TProductPlane;
import com.consumer.service.DevopsBucketService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@RestController
@RequestMapping("bucket")
@Transactional(rollbackFor = Exception.class)
public class BucketController {
    @Resource
    MinioUtil minioUtil;
    @Resource
    DevopsBucketService bucketService;
    @Resource
    ExecuteTask executeTask;

    @PostMapping("createProjectBucket")
    public void createProjectBucket() throws Exception {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProjectPlan", paramMap4);
        List<TPlane> planeList = JSON.parseArray(plans, TPlane.class);

//        HashMap<String, Object> paramMap66 = new HashMap<>();
//        paramMap66.put("systemId", "1684375517106356224");
//        String plan = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProjectPlanBySystemId", paramMap66);
//        TPlane tPlane = JSON.parseObject(plan, TPlane.class);
//
//        planeList.clear();
//        planeList.add(tPlane);

        DevopsBucket bucket = null;
        List<DevopsBucket> list = new ArrayList<>();
        for (TPlane plane : planeList) {
            if (StringUtils.isBlank(plane.getSystemId())) {
                continue;
            }
            System.out.println("项目:: "+plane.getPlaneName()+"正在创建桶...");
            String bucketId = plane.getPlaneId();
            bucket = new DevopsBucket();
            bucket.setBucketId(bucketId);
            bucket.setBucketName(plane.getPlaneName());
            bucket.setSystemType("1");
            bucket.setPlanId(bucketId);
            //先写入本地库
            list.add(bucket);
            //在写入minion
//            minioUtil.createBucket("devops-" + plane.getPlaneId());
            //TODO 初始化文件夹
            initDir(bucketId);
        }

        HashMap<String, Object> paramMap5= new HashMap<>();
        String productPlan = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProductPlan", paramMap5);
        List<TProductPlane> productPlaneList = JSON.parseArray(productPlan, TProductPlane.class);
        for (TProductPlane productPlane : productPlaneList) {
            if (StringUtils.isBlank(productPlane.getSystemId())) {
                continue;
            }
            System.out.println("产品::"+productPlane.getProductPlaneName()+"正在创建桶...");
            String bucketId = productPlane.getProductPlaneId();
            bucket = new DevopsBucket();
            bucket.setBucketId(bucketId);
            bucket.setBucketName(productPlane.getProductPlaneName());
            bucket.setSystemType("1");
            bucket.setPlanId(bucketId);
            //先写入本地库
            list.add(bucket);
            //在写入minion
            minioUtil.createBucket("devops-" + productPlane.getProductPlaneId());
            //TODO 初始化文件夹
            initDir(bucketId);
        }

        if (!list.isEmpty()) {
//            bucketService.saveBatch(list);
        }





    }


    public void initDir(String bucketId) {
        DirectoryVo build = DirectoryVo.builder().bucketFileId("milestone-" + bucketId).dirName("里程碑").dirId("milestone-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build1 = DirectoryVo.builder().bucketFileId("task-" + bucketId).dirName("任务").dirId("task-" + bucketId).dirName("任务").bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build2 = DirectoryVo.builder().bucketFileId("test-" + bucketId).dirName("测试").dirId("test-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build3 = DirectoryVo.builder().bucketFileId("demand-" + bucketId).dirName("需求池").dirId("demand-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build4 = DirectoryVo.builder().bucketFileId("pack-" + bucketId).dirName("制品").dirId("pack-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        bucketService.createDir(build);
        bucketService.createDir(build2);
        bucketService.createDir(build1);
        bucketService.createDir(build3);
        bucketService.createDir(build4);
    }


    @PostMapping("ttb")
    public void ttb() {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProjectPlan", paramMap4);
        List<TPlane> planeList = JSON.parseArray(plans, TPlane.class);
        BlockingQueue<TPlane> queue = new LinkedBlockingQueue<>(planeList);

        executeTask.createProjectBucket(queue);
    }

    @PostMapping("ttc")
    public void ttc() {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProductPlan", paramMap4);
        List<TProductPlane> planeList = JSON.parseArray(plans, TProductPlane.class);
        BlockingQueue<TProductPlane> queue = new LinkedBlockingQueue<>(planeList);

        executeTask.createProductBucket(queue);
    }

    @PostMapping("moveProjectBucketFile")
    public void moveProjectBucketFile() {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProjectPlan", paramMap4);
        List<TPlane> planeList = JSON.parseArray(plans, TPlane.class);

//        HashMap<String, Object> paramMap66 = new HashMap<>();
//        paramMap66.put("systemId", "1684375517106356224");
//        String plan = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProjectPlanBySystemId", paramMap66);
//        TPlane tPlane = JSON.parseObject(plan, TPlane.class);
//
//        planeList.clear();
//        planeList.add(tPlane);

        BlockingQueue<TPlane> queue = new LinkedBlockingQueue<>(planeList);
        executeTask.moveProjectBucketFile(queue);
    }


    @PostMapping("moveProductBucketFile")
    public void moveProductBucketFile() {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProductPlan", paramMap4);
        List<TProductPlane> planeList = JSON.parseArray(plans, TProductPlane.class);
//
//        HashMap<String, Object> paramMap66 = new HashMap<>();
//        paramMap66.put("systemId", "1684375517106356224");
//        String plan = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProjectPlanBySystemId", paramMap66);
//        TPlane tPlane = JSON.parseObject(plan, TPlane.class);
//
//        planeList.clear();
//        planeList.add(tPlane);

        BlockingQueue<TProductPlane> queue = new LinkedBlockingQueue<>(planeList);
        executeTask.moveProductBucketFile(queue);
    }
}
